﻿Public Class frmNewSection
    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim Primary_Code As Integer
#Region "Function"
    Function Get_NEW_ID() As Integer
        Try
            'Get Last Insert 
            Strsql = "SELECT LAST_INSERT_ID();"
            Dim Last_ITEM_ID As Integer = 0
            Get_NEW_ID = _mysql.MySQLExecuteScalar(Strsql)

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NEW_ID"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With

        End Try
    End Function
    Sub Load_AutoComplete_SectionName(Optional ByVal TypeCode As String = "%", Optional ByVal KindCode As String = "%", Optional ByVal Attribute As String = "%")
        Try
            Strsql = "Select Section_Name from sections where Cate_ID = '" & Me.CboCategories.SelectedValue & "'"


            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "sections")
            If DT_Category.Rows.Count = 0 Then Exit Sub
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_Category.Rows.Count - 1
                ColItemName.Add(DT_Category.Rows(i).Item("Section_Name"))
            Next

            With TxtSectionName
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .AutoCompleteCustomSource = ColItemName

            End With


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_CategoriesName"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Category()
        Try
            Strsql = "Select Cate_ID,Cate_Name from categories order by Cate_ID "
            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "categories")
            With CboCategories
                .DataSource = DT_Category
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"
                .SelectedIndex = 0
            End With
        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_Category"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try
    End Sub
    Function Validate_data() As Boolean
        Validate_data = False
        Try
            If Me.TxtSectionName.Text = "" Then
                MsgBox("ชื่อต้องไม่เป็นค่าว่าง", MsgBoxStyle.Critical, "กรุณากรอกข้อมูลให้ครบ")
                Exit Function
            End If

            If Me.TxtSectionCode.Text = "" Then
                MsgBox("กรุณาใส่เลขที่ภายใน", MsgBoxStyle.Critical, "กรุณากรอกข้อมูลให้ครบ")
                Exit Function
            End If

            Strsql = "Select count(*) as cc from sections where section_id = " & Me.TxtSectionCode.Text
            Dim DupplicateID As Integer = _mysql.MySQLExecuteScalar(Strsql)
            Select Case DupplicateID
                Case Is > 0
                    MsgBox("รหัสชนิดครุภัณฑ์นี้มีอยู่แล้ว", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                    Exit Function
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select




            Strsql = "Select count(*) as cc from sections where section_Name = '" & Me.TxtSectionName.Text & "' and Cate_ID = '" & Me.CboCategories.SelectedValue & "'"
            DupplicateID = _mysql.MySQLExecuteScalar(Strsql)
            Select Case DupplicateID
                Case Is > 0
                    MsgBox("รหัสชนิดครุภัณฑ์นี้มีอยู่แล้ว", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                    Exit Function
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select

            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
#End Region

    Private Sub frmNewSection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Category()
        CboCategories.SelectedValue = frmSection.CboCategories.SelectedValue
        Load_AutoComplete_SectionName()
    End Sub

    Private Sub GroupControl2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl2.Paint

    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            If Validate_data() = False Then Exit Sub

            If MsgBox("คุณต้องการเพิ่มข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "กรุณายืนยันการเพิ่มข้อมูล") = MsgBoxResult.No Then Exit Sub
            Dim StrCate_Code As String = ""
            Dim DT_Categories As New DataTable
            Strsql = "Select ifnull(Cate_Code,'') as Cate_Code from categories where Cate_ID = " & Me.CboCategories.SelectedValue
            DT_Categories = _mysql.GetMYSQLDataTable(Strsql, "categories")
            If DT_Categories.Rows(0).Item("Cate_Code").ToString <> "" Then
                StrCate_Code = DT_Categories.Rows(0).Item("Cate_Code").ToString


            End If

            Strsql = "Insert into  sections (Section_ID,Section_Name ,Cate_ID,Section_Code,Cate_Code) values( " & Val(Me.TxtSectionCode.Text) & ",'" & Me.TxtSectionName.Text & "'," & Me.CboCategories.SelectedValue & ",'" & Me.TxtSectionCode.Text & "','" & StrCate_Code & "')"
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With

                    Exit Sub
            End Select
            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (5,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "'," & Get_NEW_ID() & ") "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1

                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
                Case Else
                    MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการเพิ่มข้อมูล")
                    DialogResult = Windows.Forms.DialogResult.OK
                    Me.Dispose()
            End Select
           
        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "OK_Button_Click"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged

    End Sub
End Class